package com.java.day09.homework.teamPK;

import com.java.day08.homework.pk.Fightable;
import com.java.day08.homework.pk.Hero;

import java.util.Random;
import java.util.Scanner;

/**
 * 团队战斗类
 */
public class TeamDuel {
    //全部英雄
    public static Hero[] heroes = new Hero[6];
    //英雄团队1
    public static Hero[] heroesTeam1;
    //英雄团队2
    public static Hero[] heroesTeam2;

    //创建英雄并随机组队
    public static void pair() {
        Random random = new Random();
        //创建英雄并放入数组
        for (int i = 0; i < heroes.length; i++) {
            System.out.println("请玩家"+(i+1)+"创建英雄:");
            Hero hero = new Hero();
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入英雄姓名:");
            String name = scanner.next();
            hero.setName(name);


            //生命随机 1200~1500
            hero.setLife(1200 + random.nextInt(500 + 1 - 200));
            //伤害随机 100~150
            hero.setAttack(100 + random.nextInt(50 + 1));
            //打印英雄面板
            System.out.println("英雄面板:" + hero);

            heroes[i] = hero;


        }


        // 创建索引数组
        int[] indices = new int[heroes.length];
        for (int i = 0; i < indices.length; i++) {
            indices[i] = i;
        }

        // 打乱索引数组的顺序
        for (int i = indices.length - 1; i > 0; i--) {
            int index = random.nextInt(i + 1);
            // 交换当前索引和随机索引处的元素
            int temp = indices[index];
            indices[index] = indices[i];
            indices[i] = temp;
        }

        // 拆分数组
        heroesTeam1 = new Hero[heroes.length / 2];
        heroesTeam2 = new Hero[heroes.length / 2];
        for (int i = 0; i < indices.length / 2; i++) {
            heroesTeam1[i] = heroes[indices[i]];
            heroesTeam2[i] = heroes[indices[i + indices.length / 2]];
        }


        System.out.println("所有英雄:");
        for (int i = 0; i < heroes.length; i++) {
            System.out.println(heroes[i]);
        }
        System.out.println();

        System.out.println("队伍1英雄:");
        for (int i = 0; i < heroesTeam1.length; i++) {
            System.out.println(heroesTeam1[i]);
        }
        System.out.println();

        System.out.println("队伍2英雄:");
        for (int i = 0; i < heroesTeam2.length; i++) {
            System.out.println(heroesTeam2[i]);
        }
        System.out.println();


    }



    public static void teamPK(Fightable[] team1,Fightable[] team2){
        //回合数
        int count = 1;
        System.out.println("开始对战");
        while(true){
            if(Team.TeamIsAlive(team1)&&Team.TeamIsAlive(team2)){
                System.out.println("第"+(count++)+"回合");
                //哪个队伍先手
                Random random = new Random();
                int i = random.nextInt(2);
                if(i==0){
                    //team1先手
                    System.out.println("队伍1先手");
                    Team.TeamFight(team1,team2);
                    if (Team.TeamIsAlive(team2)){
                        Team.TeamFight(team2,team1);
                    }
                }
                if (i==1){
                    //team2先手
                    System.out.println("队伍2先手");
                    Team.TeamFight(team2,team1);
                    if (Team.TeamIsAlive(team1)){
                        Team.TeamFight(team1,team2);
                    }
                }
            }else{
                if(Team.TeamIsAlive(team1)){
                    System.out.println("队伍1获胜");
                    return;
                }
                System.out.println("队伍2获胜");
                return;
            }

        }
    }

}
